//
//  SEGActivityLotteryRankListCell.m
//  ExcellentHome
//
//  Created by 揭婧雯 on 2017/8/15.
//  Copyright © 2017年 陈区. All rights reserved.
//

#import "SEGActivityLotteryRankListCell.h"

@interface SEGActivityLotteryRankListCell ()

@property (nonatomic, strong) UIView* lineView;
@property (nonatomic, strong) UIView* listContentView;
/**
 是否已经展示全部
 */
@property (nonatomic, assign) BOOL hasShowAll;
/**
 展开按钮
 */
@property (nonatomic, strong)UIButton *showAllButton;

@end

@implementation SEGActivityLotteryRankListCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setupUI];
    }
    return self;
}

- (void)setupUI
{
    self.backgroundColor = kColor_W;
    
    UILabel *label = [[UILabel alloc] init];
    label.font = kFont_13;
    label.textColor = kColor_1;
    label.text = @"排行榜";
    [self.contentView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(15);
        make.top.equalTo(self.contentView);
        make.height.mas_equalTo(44.0);
    }];
    
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = kColor_L2;
    [self.contentView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView);
        make.right.equalTo(self.contentView);
        make.top.equalTo(label.mas_bottom);
        make.height.mas_equalTo(0.5);
    }];
    self.lineView = line;
    self.hasShowAll = NO;
}

- (void)setUpCellWithLotteryDetailModel:(SEGActivityLotteryDetailModel *)model
{
    [self.listContentView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperview];
    }];
    
    [model.lotteryUsersSort enumerateObjectsUsingBlock:^(SEGActivityLotteryUsersSortModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIImageView *iconView = [[UIImageView alloc] init];
        [iconView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kClientImageRootURL,obj.userPic]] placeholderImage:[UIImage imageWithContentsOfFile:SEGPicName(@"headimg160")]];
        iconView.layer.masksToBounds = YES;
        [self.listContentView addSubview:iconView];
        [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.listContentView).offset(54);
            make.top.equalTo(self.lineView.mas_bottom).offset(15 + (53 * idx));
            make.width.height.mas_equalTo(38);
        }];
        [iconView seg_configWithRadius:19];
        UILabel *number;
        if (idx < 3) {//前三个
            NSString *imageName = [NSString stringWithFormat:@"img_activity_medal0%@",@(idx + 1)];
            UIImageView *medalView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:SEGPicName(imageName)]];
            [self.listContentView addSubview:medalView];
            [medalView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.listContentView).offset(15);
                make.centerY.equalTo(iconView);
                make.width.mas_equalTo(24);
            }];
        }else {
            number = [[UILabel alloc] init];
            number.font = kFont_13;
            number.textColor = kColor_1;
            number.textAlignment = NSTextAlignmentCenter;
            number.text = [NSString stringWithFormat:@"%@",@(idx + 1)];
            [self.listContentView addSubview:number];
            [number mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.listContentView).offset(15);
                make.centerY.equalTo(iconView);
                make.width.mas_equalTo(24);
            }];
        }
        
        UILabel *namelabel = [[UILabel alloc] init];
        namelabel.font = kFont_13;
        namelabel.textColor = kColor_1;
        namelabel.text = obj.nickName;
        [self.listContentView addSubview:namelabel];
        
        UILabel *prizeLabel = [[UILabel alloc] init];
        prizeLabel.font = kFont_13;
        prizeLabel.textColor = kColor_1;
        prizeLabel.text = obj.prizeName;
        prizeLabel.textAlignment = NSTextAlignmentRight;
        [self.listContentView addSubview:prizeLabel];

        [namelabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(iconView.mas_right).offset(15);
            make.width.mas_equalTo(ceil((LYScreenWidth() - 115)*0.4));
            make.centerY.equalTo(iconView);
        }];
        
        [prizeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.listContentView.mas_right).offset(-15);
            make.centerY.equalTo(iconView);
            make.left.equalTo(namelabel.mas_right).offset(8);
        }];
        
        if (self.hasShowAll) {
            if (idx == model.lotteryUsersSort.count - 1) {
                [self.listContentView mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.bottom.equalTo(namelabel.mas_bottom).offset(15);
                }];
                [self.showAllButton mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.height.mas_equalTo(36.5);
                }];
                [self.showAllButton setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"btn_list_up")] forState:UIControlStateNormal];
            }
        }else {//没显示所有的 最后一个显示的
            if (model.lotteryUsersSort.count > 3) {
                if (idx == 2) {
                    [self.listContentView mas_updateConstraints:^(MASConstraintMaker *make) {
                        make.bottom.equalTo(namelabel.mas_bottom).offset(15);
                    }];
                    [self.showAllButton mas_updateConstraints:^(MASConstraintMaker *make) {
                        make.height.mas_equalTo(36.5);
                    }];
                    [self.showAllButton setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"btn_list_down")] forState:UIControlStateNormal];
                    *stop = YES;
                }
            }else {
                if (idx == model.lotteryUsersSort.count - 1) {//少于三个是最后一个
                    [self.listContentView mas_updateConstraints:^(MASConstraintMaker *make) {
                        make.bottom.equalTo(iconView.mas_bottom).offset(15);
                    }];
                    //个数小于三个不显示箭头
                    [self.showAllButton mas_updateConstraints:^(MASConstraintMaker *make) {
                        make.height.mas_equalTo(0);
                    }];
                    *stop = YES;
                }
            }
        }
    }];
}

- (void)seg_showAllButtonPressed
{
    self.hasShowAll = !self.hasShowAll;
    if (self.delegate && [self.delegate respondsToSelector:@selector(showButtonAction)]) {
        [self.delegate showButtonAction];
    }
}

- (UIButton *)showAllButton
{
    if (!_showAllButton) {
        _showAllButton = [[UIButton alloc] init];
        [_showAllButton setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"btn_list_down")] forState:UIControlStateNormal];
        [_showAllButton addTarget:self action:@selector(seg_showAllButtonPressed) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_showAllButton];
        [_showAllButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.contentView);
            make.top.equalTo(self.listContentView.mas_bottom).offset(0);
            make.bottom.equalTo(self.contentView).offset(-0);
            make.width.mas_equalTo(36.5);
            make.height.mas_equalTo(36.5);//图片高度6.5 上下间距各15
        }];
    }
    return _showAllButton;
}

- (UIView *)listContentView
{
    if (!_listContentView) {
        _listContentView = [[UIView alloc] init];
        [self.contentView addSubview:_listContentView];
        [_listContentView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.contentView);
            make.top.equalTo(self.lineView);
        }];
    }
    return _listContentView;
}

@end
